package com.opdar.athena.singletask;

import com.opdar.athena.base.Command;
import com.opdar.athena.base.Constants;
import com.opdar.athena.client.agent.base.Configuration;
import com.opdar.athena.singletask.base.SingleTaskContextListener;
import com.opdar.platform.core.base.Context;
import com.opdar.platform.core.base.DispatcherServlet;
import com.opdar.platform.core.base.ServletEventListener;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * Created by shiju on 2017/8/16.
 */
public class SingleTaskServer {

    public static void main(String[] args) {
        Context.putResourceMapping("/css","classpath:/static/css");
        Context.putResourceMapping("/fonts","classpath:/static/fonts");
        Context.putResourceMapping("/imgs","classpath:/static/imgs");
        Context.putResourceMapping("/js","classpath:/static/js");
        Map<String, String> values = Command.valueOf(args);
        if(values.containsKey("--config")){
            Constants.GLOBAL_PROPERTIES = values.get("--config");
        }
        int wPort = 8080;
        int sPort = 18181;
        if(values.containsKey("--wport")){
            String port = values.get("--wport");
            if(!StringUtils.isEmpty(port)){
                wPort = Integer.valueOf(port);
            }
        }
        if(values.containsKey("--sport")){
            String port = values.get("--sport");
            if(!StringUtils.isEmpty(port)){
                sPort = Integer.valueOf(port);
            }
        }
        Configuration.getConfiguration().setPort(sPort);
        Server server = new Server(wPort);
        ServletContextHandler context = new ServletContextHandler(1);
        context.addEventListener(new ServletEventListener());
        context.addEventListener(new SingleTaskContextListener());
        context.setContextPath("/");
        context.addServlet(DispatcherServlet.class, "/");
        server.setHandler(context);
        try {
            server.start();
            server.join();
        } catch (Exception var3) {
            var3.printStackTrace();
        }
    }
}
